package com.menggudf.service.impl;

import com.menggudf.entity.Duration;
import com.menggudf.mapper.DurationDao;
import com.menggudf.service.DurationService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (Duration)表服务实现类
 *
 * @author makejava
 * @since 2025-02-20 16:58:27
 */
@Service("durationService")
public class DurationServiceImpl implements DurationService {
    @Resource
    private DurationDao durationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Duration queryById(Integer id) {
        return this.durationDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param duration 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Duration> queryByPage(Duration duration, PageRequest pageRequest) {
        long total = this.durationDao.count(duration);
        return new PageImpl<>(this.durationDao.queryAllByLimit(duration, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param duration 实例对象
     * @return 实例对象
     */
    @Override
    public Duration insert(Duration duration) {
        this.durationDao.insert(duration);
        return duration;
    }

    /**
     * 修改数据
     *
     * @param duration 实例对象
     * @return 实例对象
     */
    @Override
    public Duration update(Duration duration) {
        this.durationDao.update(duration);
        return this.queryById(duration.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.durationDao.deleteById(id) > 0;
    }
}
